package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.Quality;
import com.ruoyi.system.service.IContractService;
import com.ruoyi.system.service.ICostService;
import com.ruoyi.system.service.IProjectService;
import com.ruoyi.system.service.IQualityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检单 信息操作处理
 *
 * @author ruoyi
 * @date 2018-12-24
 */
@Controller
@RequestMapping("/system/quality")
public class QualityController extends BaseController {
    private String prefix = "system/quality" ;

    @Autowired
    private IQualityService qualityService;
    @Autowired
    private IContractService contractService;
    @Autowired
    private ICostService costService;

    @RequiresPermissions("system:quality:view")
    @GetMapping()
    public String quality() {
        return prefix + "/quality" ;
    }

    /**
     * 查询质检单列表
     */
    @RequiresPermissions("system:quality:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Quality quality) {
        startPage();
        List<Quality> list = qualityService.selectQualityList(quality);

        return getDataTable(list);
    }


    /**
     * 导出质检单列表
     */
    @RequiresPermissions("system:quality:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Quality quality) {
        List<Quality> list = qualityService.selectQualityList(quality);
        ExcelUtil<Quality> util = new ExcelUtil<Quality>(Quality.class);
        return util.exportExcel(list, "quality");
    }

    /**
     * 新增质检单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("contractList", contractService.selectContractList(null));
        mmap.put("costList", costService.selectCostList(null));
        return prefix + "/add" ;
    }

    /**
     * 新增保存质检单
     */
    @RequiresPermissions("system:quality:add")
    @Log(title = "质检单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Quality quality) {
        return toAjax(qualityService.insertQuality(quality));
    }

    /**
     * 修改质检单
     */
    @GetMapping("/edit/{qualityId}")
    public String edit(@PathVariable("qualityId") Integer qualityId, ModelMap mmap) {
        Quality quality = qualityService.selectQualityById(qualityId);
        mmap.put("quality", quality);
        return prefix + "/edit" ;
    }

    /**
     * 修改保存质检单
     */
    @RequiresPermissions("system:quality:edit")
    @Log(title = "质检单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Quality quality) {
        return toAjax(qualityService.updateQuality(quality));
    }

    /**
     * 删除质检单
     */
    @RequiresPermissions("system:quality:remove")
    @Log(title = "质检单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(qualityService.deleteQualityByIds(ids));
    }

}
